<template>
  <el-dialog :title="'编辑签到记录'" :visible.sync="visible" width="600px" :close-on-click-modal="false" append-to-body>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" size="small" label-width="120px" label-position="right">
      <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="老师姓名">
            <el-input v-model="dataForm.teacherName" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签到日期">
            <el-input v-model="dataForm.signDate" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签到时段">
            <el-input v-model="dataForm.period" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签到状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="请选择状态" :style="{ width: '100%' }">
              <el-option label="正常签到" value="Normal" />
              <el-option label="迟到" value="Late" />
              <el-option label="请假" value="Leave" />
              <el-option label="早退" value="EarlyLeave" />
              <el-option label="缺勤" value="Absent" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签到方式" prop="method">
            <el-select v-model="dataForm.method" placeholder="请选择方式" :style="{ width: '100%' }">
              <el-option label="手动签到" value="MANUAL" />
              <el-option label="扫码签到" value="QR_CODE" />
              <el-option label="人脸识别" value="FACE_RECOGNITION" />
              <el-option label="指纹识别" value="FINGERPRINT" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="签到时间" prop="signTime">
            <el-date-picker
              v-model="dataForm.signTime"
              type="datetime"
              placeholder="请选择签到时间"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="dataForm.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateSignRecord } from '@/api/campus/trainSignRecord'

export default {
  data() {
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: '',
        teacherName: '',
        signDate: '',
        period: '',
        status: '',
        method: '',
        signTime: '',
        remark: ''
      },
      dataRule: {
        status: [{ required: true, message: '签到状态不能为空', trigger: 'change' }],
        method: [{ required: true, message: '签到方式不能为空', trigger: 'change' }],
        signTime: [{ required: true, message: '签到时间不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(data) {
      this.dataForm.id = data.id
      this.dataForm.teacherName = data.teacherName
      this.dataForm.signDate = data.signDate
      this.dataForm.period = this.getPeriodText(data.period)
      this.dataForm.status = data.status
      this.dataForm.method = data.method
      this.dataForm.signTime = data.signTime
      this.dataForm.remark = data.remark || ''
      this.visible = true
    },
    getPeriodText(period) {
      const periodMap = {
        'AM': '上午',
        'PM': '下午',
        'EVE': '晚上'
      }
      return periodMap[period] || period
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          const params = {
            id: this.dataForm.id,
            status: this.dataForm.status,
            method: this.dataForm.method,
            signTime: this.dataForm.signTime,
            remark: this.dataForm.remark
          }
          updateSignRecord(params).then(() => {
            this.$message.success('修改成功')
            this.visible = false
            this.$emit('refresh')
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 15px;
}
</style>